package org.tbjd.client.customWidgets;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.interfaces.Decisionmaker;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Widget;

public class CollapseButton extends ToggleButton implements Decisionmaker {
	
	private Widget source = null;
	private Map<Integer, Object> map = new HashMap<Integer, Object>();

	public CollapseButton(final Task task, Image createImage, Image createImage2) {
		super(createImage, createImage2);

		addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				source = sender;
				
				map = new HashMap<Integer, Object>();
				map.put(NotificationsConstants.TASKLIST_COLLAPSE, task);
				
				try {
					delegate(map);
					
				} catch (TBJDException e) {
					e.printStackTrace();
					
				}
			}
		});

	}

	public void decide(Object action) throws TBJDException {
	}

	public void handle(Object action) throws TBJDException {
	}

	public void delegate(Object action) throws TBJDException {
		System.out.println("CollapseButton.delegate()");

		// passes info to the first responsible
		Widget parent = ((Widget) source).getParent();
		while (parent != null && parent instanceof Decisionmaker == false) {
			parent = ((Widget) parent).getParent();
		}
		if (parent != null) {
			try {
				((Decisionmaker) parent).decide(action);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		} else {
			throw new NoUpperDecisionmakerFoundException();
		}
	}

}
